package java复习202407;

import org.junit.jupiter.api.Test;

public class c1_String相关验证 {

    @Test
    public void 验证String地址(){
        String a = "a";
        String b = "a";
        String c = new String("a");
        Object o = new Object();
        System.out.println(System.identityHashCode(a));
        System.out.println(System.identityHashCode(b));
        System.out.println(System.identityHashCode(c));
        System.out.println(System.identityHashCode(0));
    }

    @Test
    public void 验证String创建多少个对象(){
        String a = "a";
        String b = a + "b";

    }

    @Test
    public void 验证StringBuild和StringBuffer(){
        String a = "a";
        StringBuilder sb = new StringBuilder("sb");

        String sba = a + sb;
        System.out.println(sba);

        StringBuilder sbadd = sb.append(a);
        System.out.println(sbadd);

        StringBuffer bf = new StringBuffer("bf");
        StringBuffer bfadd = bf.append(a);
        System.out.println(bfadd);

    }

    @Test
    public void toString和equals验证(){
        String a = "a你好";
        String b = "b";
        String s = a.toString();
        boolean equals = a.equals(b);
        int i = a.hashCode();
        System.out.println(i);

        int i1 = this.hashCode();
        System.out.println(i1);

    }

    @Test
    public void 字符串剪切验证(){
        String str = "a|b|c|";
        String substr1 = str.substring(0, str.lastIndexOf("|"));
        System.out.println(substr1);

        substr1 += "|";
        System.out.println(substr1);

        if (substr1.substring(substr1.length()-1) == "|"){
            substr1 = substr1.substring(0, substr1.lastIndexOf("|"));
        }
        System.out.println(substr1);

        String substr2 = substr1.substring(0, substr1.lastIndexOf("|"));
        System.out.println(substr2);


    }
}
